<?php

namespace App\Music\Faith;

/**
 * tencent_format类用于格式化QQ音乐接口返回的数据。
 */
class TencentFormat
{

    protected string $source = 'tencent';

    /**
     * 根据搜索类型重构数据
     *
     * @param string $search_type 搜索类型
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function search(string $search_type, array $data): array
    {
        var_dump('search' . ucfirst($search_type));
        $func = 'search' . ucfirst($search_type);
        return $this->$func($data);
    }

    /**
     * 搜索单曲
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function searchSong(array $data): array
    {
        return array_map(function ($item) {
            $result = [
                'id' => $item['mid'],
                'name' => $item['name'],
                'artist' => array_map(function ($artist) {
                    return ['id' => $artist['mid'], 'name' => $artist['name']];
                }, $item['singer'] ?? []),
                'album_id' => $item['album']['mid'] ?? '',
                'album_name' => $item['album']['name'] ?? '',
                'source' => $this->source,
            ];

            return $result;
        }, $data);
    }


    /**
     * 搜索歌手
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function searchSinger(array $data): array
    {
        return array_map(function ($datum) {
            return [
                'id' => $datum['singerMID'],
                'name' => $datum['singerName'],
                'avatar' => $datum['singerPic'],
                'song_num' => $datum['songNum'],
                'album_num' => $datum['albumNum'],
                'mv_num' => $datum['mvNum'],
                'source' => $this->source,
            ];
        }, $data);
    }

    /**
     * 搜索专辑
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function searchAlbum(array $data): array
    {
        return array_map(function ($datum) {
            return [
                'id' => $datum['albumMID'],
                'name' => $datum['albumName'],
                'avatar' => $datum['albumPic'],
                'singer_list' => $datum['singer_list'],
                'artist' => array_map(static function ($artist) {
                    return ['id' => $artist['mid'], 'name' => $artist['name']];
                }, $datum['singer_list'] ?? []),
                'song_num' => $datum['song_count'],
                'publish_date' => $datum['publicTime'],
                'source' => $this->source,
            ];
        }, $data);
    }

    /**
     * 搜索歌单
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function searchSonglist(array $data): array
    {
        return array_map(function ($datum) {
            return [
                'id' => $datum['dissid'],
                'name' => $datum['dissname'],
                'avatar' => $datum['imgurl'],
                'introduction' => $datum['introduction'],
                'listen_num' => $datum['listennum'],
                'song_count' => $datum['song_count'],
                'source' => $this->source,
            ];
        }, $data);
    }

    /**
     * 搜索MV
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function searchMv(array $data): array
    {
        return array_map(function ($datum) {
            return [
                'id' => $datum['mv_id'],
                'name' => $datum['mv_name'],
                'avatar' => $datum['mv_pic_url'],
                'artist' => array_map(static function ($artist) {
                    return ['id' => $artist['mid'], 'name' => $artist['name']];
                }, $datum['singer_list'] ?? []),
                'hilight' => $datum['singerName_hilight'],
                'source' => $this->source,
            ];
        }, $data);
    }


    /**
     * 重构专辑数据
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function album(array $data): array
    {
        $album = [
            'id' => $data['album_mid'],
            'name' => $data['album_name'],
            'artist' => array_map(function ($artist) {
                return ['id' => $artist['singermid'], 'name' => $artist['singername']];
            }, $data['singerinfo']),
            'desc' => $data['desc'],
            'pic_url' => $data['headpiclist'][0]['picurl'] ?? '',
            'song_list' => [],
            'source' => $this->source,
        ];

        foreach ($data['songlist'] as $item) {
            $song = [
                'id' => $item['songmid'],
                'name' => $item['songname'],
                'artist' => array_map(function ($artist) {
                    return ['id' => $artist['mid'], 'name' => $artist['name']];
                }, $item['singer']),
                'album_name' => trim($data['album_name']),
                'avatar_id' => $item['songmid'],
                'lyric_id' => $item['songmid'],
                'source' => $this->source,
            ];
            $album['song_list'][] = $song;
        }

        return $album;
    }


    /**
     * 格式化歌单数据
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function playList(array $data): array
    {
        return [
            'id' => $data['disstid'],
            'name' => $data['dissname'],
            'avatar' => $data['logo'],
            'desc' => $data['desc'],
            'song_ids' => $data['songids'],
            'song_types' => $data['songtypes'],
            'song' => $data['songlist'],
            'source' => $this->source,
        ];
    }

    /**
     * 格式化歌曲数据
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function song(array $data): array
    {
        $result = [
            'id' => $data['mid'],
            'name' => $data['name'],
            'artist' => array_map(static function ($artist) {
                return ['id' => $artist['mid'], 'name' => $artist['name']];
            }, $data['singer']),
            'album' => trim($data['album']['title']),
            'pic_id' => $data['album']['mid'],
            'url_id' => $data['mid'],
            'lyric_id' => $data['mid'],
            'file' => $this->songFile($data['file']),
            'source' => $this->source,
        ];

        return $result;
    }

    /**
     * 格式化歌曲文件格式
     *
     * @param array $file 文件数据
     * @return array
     */
    protected function songFile(array $file): array
    {
        $format_list = [
            'size_96aac' => 'm4a',
            'size_128mp3' => '128',
            'size_320mp3' => '320',
            'size_ape' => 'ape',
            'size_flac' => 'flac',
            'size_hires' => 'Hi-Res',
        ];

        return array_filter($format_list, static function ($key) use ($file) {
            return array_key_exists($key, $file) && $file[$key] > 0;
        }, ARRAY_FILTER_USE_KEY);
    }

    /**
     * 格式化榜单详情数据
     *
     * @param array $data QQ音乐返回数据
     * @return array
     */
    public function top(array $data): array
    {
        $result = [
            'top_id' => $data['topId'],
            'title' => $data['title'],
            'intro' => $data['intro'],
            'update_time' => $data['updateTime'],
            'listen_num' => $data['listenNum'],
            'total_num' => $data['totalNum'],
            'head_pic_url' => $data['headPicUrl'],
            'top_album_url' => $data['topAlbumURL'],
            'source' => $this->source,
        ];

        // 处理歌曲数据
        $result['song'] = array_map(static function ($song) {
            return [
                'rank' => $song['rank'],
                'album_mid' => $song['albumMid'],
                'title' => $song['title'],
                'singer_name' => $song['singerName'],
                'cover' => $song['cover'],
            ];
        }, $data['song']);

        return $result;
    }

}